﻿/// <reference path="jquery.js" />

function WSWebSocket(url, callback, outerContainerID, callBackWhenDataNull) {

    if (arguments.length < 2) {
        throw "需要提供web socket的clientpath 以及回调方法";
    }

    var ws = new WebSocket(url);
    ws.onmessage = function (evt) {
        var obj = JSON.parse(evt.data);
        var isNull;
        if (obj instanceof Array) {
            isNull = obj.CheckNull();
        }
        if (isNull) {
            if (callBackWhenDataNull) {
                callBackWhenDataNull(obj);
                return;
            } else if (outerContainerID) {
                //默认显示没有数据
                setDefaultWhenNull(outerContainerID);
                return;
            }
        }
        callback(obj);
    };

    ws.onopen = function () {
        console.log(url + " opened at " + new Date());
    };

    ws.onclose = function () {
        console.log(url+' closed at ' + new Date());
        console.log("trying to reconnect....");
        //when iis session time out we need to active it first by request a file
        var intervalId = setInterval(function () {
            ajaxRequest("/", function () {
                clearInterval(intervalId);
                WSWebSocket(url, callback, outerContainerID, callBackWhenDataNull);
            });

        }, 5000);
    }
}

function ajaxRequest(url, callback) {
    $.ajax({
        url: url,
        async: false
    }).done(function (data) {
        if (callback) {
            callback(data);
        }
    }).fail(function (e) {
    })
}

Array.prototype.CheckNull = function () {
    var arr = this;
    if (arr.length == 0) {
        return true;
    }
    if (arr.length == 1) {
        if (arr[0] instanceof Array) {
            return arr[0].CheckNull();
        }
    }
    return false;
};

Array.prototype.quickSort = function quickSort(comparer) {
    var arr = this;
    if (arr.length <= 1) { return arr; }
    var pivotIndex = Math.floor(arr.length / 2);
    var pivot = arr.splice(pivotIndex, 1)[0];
    var left = [];
    var right = [];
    for (var i = 0; i < arr.length; i++) {

        var item1 = arr[i];
        var item2 = pivot;
        if (comparer) {
            item1 = comparer(arr[i]);
            item2 = comparer(pivot);
        }
        if (item1 < item2) {
            left.push(arr[i]);
        } else {
            right.push(arr[i]);
        }
    }
    left = left.quickSort(comparer);
    right = right.quickSort(comparer);
    return left.concat([pivot], right);
};

function setDefaultWhenNull(outerContainerID) {
    var container = $("#" + outerContainerID);
    container.css("text-align", "center");
    container.css("vertical-align", "middle");
    var height = container.height();
    if (height == 0) {
        height = 180;
    }
    container.css("line-height", height + "px");
    container.css("font-size", 22 + "px");
    container.css("font-weight", 500);
    container.html("暂无数据");
}

Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function formateTime(createDate) {
    return createDate.replace('T', ' ');
    // /Date(1386569365000+0800)/
    //var d = new Date(parseInt(createDate.substr(6)));
    //return d.Format('yyyy-MM-dd hh:mm:ss');
}

function ShowMessage(msg, callback) {
    $.Zebra_Dialog(msg, { onClose: function () {
        if (callback) {
            callback.call();
        }
    }
    });
}
